package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("templateController")
public class TemplateController {
    @Reference
    private TemplateService templateService;
    @Reference
    private BrandService brandService;
    @Reference
    private SpecificationService specificationService;
    //跳转模板管理页面
    @RequestMapping("toTemplate")
    public String toTemplate(Model model){
        //查询品牌
        List<TbBrand> tbBrands = brandService.selectBrandall();
        model.addAttribute("tbBrands",tbBrands);
        //查询规格
        List<TbSpecification> tbSpecification = specificationService.findAll("");
        model.addAttribute("tbSpecification",tbSpecification);
        return "admin/type_template";
    }


    //查询模板管理所有信息
    @RequestMapping("findall")
    @ResponseBody
    public StatusCode findAll(String name){
        List<TbTypeTemplate> list = templateService.findAll(name);
        if (list == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }

    //增加模板管理
    @RequestMapping("insertTemplate")
    @ResponseBody
    public StatusCode insertTemplate(TbTypeTemplate tbTypeTemplates){
        if (tbTypeTemplates == null){
            StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = templateService.insertTemplate(tbTypeTemplates);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //查询当前模板管理对象
    @RequestMapping("selectById")
    @ResponseBody
    public StatusCode selectById(Long id){
        if (id == null){
            StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbTypeTemplate tbTypeTemplate = templateService.selectById(id);
        if (tbTypeTemplate == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS).setT(tbTypeTemplate);
    }

    //修改
    @RequestMapping("updateTemplate")
    @ResponseBody
    public StatusCode updateTemplate(TbTypeTemplate tbTypeTemplates){
        if (tbTypeTemplates == null){
            StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = templateService.updateTemplate(tbTypeTemplates);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //删除
    @RequestMapping("deleteById")
    @ResponseBody
    public StatusCode deleteById(Long[] ids){
        if (ids == null){
            StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = templateService.deleteById(ids);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }
}
